'''
Created on 15/12/2012

@author: Jorge
'''

class Example(object):
    '''
    classdocs
    '''

    categories = []

    def __init__(self,X, y):
        '''
        Constructor
        '''
        """if (type(X)==list):
            sparse = {}
            #sparse[-1]=0.0
            for i in xrange(len(X)):
                if X[i]!=0:
                    sparse[i]=X[i]
            self.X= sparse
        elif (type(X)==dict):"""
        self.X = X
        
        if type(y)==str:
            self.y = y.replace(' ','_')
        else:
            self.y = y
        self.update_categories()
    
    def get_X(self):
        return self.X
    
    def get_y(self):
        return self.y
    
    def save(self, output):
        output.write(self.y+'\t'+self.description+"\n")
        
    def update_categories(self):
        if self.y not in Example.categories:
            Example.categories.append(self.y)
            
    def get_numerical_category(self):
        return Example.categories.index(self.y)
    
    @staticmethod
    def get_number_label(label):
        return Example.categories.index(label)
    
    def get_vector_X(self):
        
        """"v=[]
        indexes = self.X.keys()
        indexes.sort()
        for i in indexes:
            v.append(self.X[i])
        return v"""
        return self.X
    
    """def sparse_data(self):
        sparse = {}
        #sparse[-1]=0.0
        
        for i in xrange(len(self.X)):
            if self.X[i]!=0:
                sparse[i]=self.X[i]
        return sparse
        return self.X"""
    